﻿using System.Collections.Generic;
using System.Linq;
using XQ.WeChatOfficialAccount.DAL.DAL;
using XQ.WeChatOfficialAccount.DAL;
using XQ.WeChatOfficialAccount.DAL.Model;
using XQ.Framework.core;
using XQ.Framework.io.text;
using System;

namespace XQ.WeChatOfficialAccount.BLL.Sys
{
    public class MenuBll
    {
        private static readonly MenuBll bll = new MenuBll();
        public static MenuBll Instance { get { return bll; } }

        private ZTreeModel model = null;
        private int menu_count = 0;
        public string GetMenuJson()
        {
            model = new ZTreeModel() { Root = new List<ZTreeModel.Node>() };
            menu_count = 0;
            using (MenuDal dal = new MenuDal())
            {
                List<Sys_Menus> firstLevel = dal.GetPartentMenuses();
                model.Root = new List<ZTreeModel.Node>();
                MenuJson(dal, firstLevel, model.Root);
            }
            return model.GetJson();
        }

        private void MenuJson(MenuDal dal, List<Sys_Menus> list, List<ZTreeModel.Node> nodes)
        {
            if (menu_count > 350) { return; }
            if (dal == null || !ValidateHelper.IsPlumpList(list)) { return; }
            Sys_Menus menu = null;
            for (int i = 0; i < list.Count; ++i)
            {
                if ((menu = list[i]) == null) { continue; }
                ZTreeModel.Node node = new ZTreeModel.Node()
                {
                    name = menu.MenuName,
                    id=menu.ID.ToString(),
                    ID = menu.ID.ToString(),
                    MenuName = menu.MenuName,
                    Linkurl = menu.Linkurl,
                    Sortnum = (menu.Sortnum ?? 0).ToString(),
                    IsVisible = ((menu.IsVisible ?? false) ? "1" : "0"),
                    ParentID = (menu.ParentID ?? 0).ToString(),
                    NavTag = menu.NavTag,
                    iconCls = menu.iconCls,
                    iconUrl = menu.iconUrl,
                    BigImageUrl = menu.BigImageUrl,
                    IsRefresh = (menu.IsRefresh??false)?"1":"0",
                    InvokingMothed = menu.InvokingMothed,
                    Component = menu.Component,
                    children = new List<ZTreeModel.Node>()
                };
                nodes.Add(node);
                List<Sys_Menus> children = dal.GetSubMenuses(menu.ID);
                if (ValidateHelper.IsPlumpList(children))
                {
                    MenuJson(dal, children, node.children);
                }
                ++menu_count;
            }
        }

        public string UpdateMenu(int id, string name, string url, int sortnum, bool isvisible, string navtag, string iconcls, string iconurl, string bigimageurl, bool isRefresh, string invokingMothed)
        {
            throw new NotImplementedException();
        }

        public string DeleteMenu(int id)
        {
            using (MenuDal dal = new MenuDal())
            {
                if (dal.ExistModel(x => x.ParentID == id))
                {
                    return "请先删除此项的子节点";
                }
                if (dal.DeleteModel(x => x.ID == id) > 0)
                {
                    return "ok";
                }
            }
            return "删除失败";
        }

        public string UpdateMenu(
            int id, string name, string url, int sortnum, bool isvisible,
            string navtag, string iconcls, string iconurl, string bigimageurl,bool IsRefresh,string InvokingMothed, string component)
        {
            int count = 0;
            using (MenuDal dal = new MenuDal())
            {
                Sys_Menus menu = dal.FindModel(x => x.ID == id);
                if (menu == null)
                {
                    return "编辑内容不存在";
                }
                if (dal.ExistModel(x => x.ID != id && x.ParentID == menu.ParentID && x.MenuName == name))
                {
                    return "同级存在重名";
                }
                count = dal.UpdateModel(x => x.ID == id, delegate(Sys_Menus model)
                {
                    model.MenuName = name;
                    model.Linkurl = url;
                    model.Sortnum = sortnum;
                    model.IsVisible = isvisible;
                    model.NavTag = navtag;
                    model.iconCls = iconcls;
                    model.iconUrl = iconurl;
                    model.BigImageUrl = bigimageurl;
                    model.IsRefresh = IsRefresh;
                    model.InvokingMothed = InvokingMothed;
                    model.Component = component;
                    return model;
                });
            }
            return count > 0 ? "ok" : "err";
        }

        public string AddMenu(string name, string url, int? sortnum, bool isvisible, int? parentid,
            string navtag, string iconcls, string iconurl, string bigimageurl, bool IsRefresh, string InvokingMothed, string component)
        {
            using (MenuDal dal = new MenuDal())
            {
                if (dal.ExistModel(x => x.ParentID == parentid && x.MenuName == name))
                {
                    return JsonHelper.ObjectToJson(new { ok = "err", msg = "同级存在重名" ,id=""});
                }
                Sys_Menus model = new Sys_Menus()
                {
                    MenuName = name,
                    Linkurl = url,
                    Sortnum = sortnum,
                    IsVisible = isvisible,
                    ParentID = parentid,
                    NavTag = navtag,
                    iconCls = iconcls,
                    iconUrl = iconurl,
                    BigImageUrl = bigimageurl,
                    IsRefresh = IsRefresh,
                    InvokingMothed = InvokingMothed,
                    Component = component
                };

                Sys_Menus newMenu = dal.AddModel(model);
                return JsonHelper.ObjectToJson(new { ok = newMenu != null ? "ok" : "error", msg = "", id = model.ID.ToString() });
            }
        }

        /// <summary>
        /// 根据菜单ID获取菜单详情
        /// </summary>
        /// <param name="id">菜单ID</param>
        /// <returns></returns>
        public Sys_Menus GetMenuByID(int id)
        {
            using (MenuDal dal = new MenuDal())
            {
                return dal.GetMenuByID(id);
            }
        }

    }
}
